/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PencarianPegawai.java
 *
 * Created on Jun 23, 2013, 4:16:11 PM
 */
package View;

import Controller.ControlData;
import Model.PNS;
import TableModel.CariPegawaiTableModel;
import java.awt.Color;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author a9uszT
 */
public class PencarianPegawai extends javax.swing.JFrame {

    String actioon;

    /** Creates new form PencarianPegawai */
    public PencarianPegawai() {
        initComponents();
        cari_txt.requestFocus();
//        jTable1.setVisible(false);
        Clock clock = new Clock();
        clock.showDigitalClock(time);
        cari_txt.setVisible(false);
        jInternalFrame_proses.setVisible(false);
//        tampilTable();
    }

//    public PencarianPegawai(String act) {
//        initComponents();
//        this.actioon = act;
//        cari_txt.requestFocus();
//        jTable1.setVisible(false);
//        Clock clock = new Clock();
//        clock.showDigitalClock(time);
//        cari_txt.setVisible(false);
//        jInternalFrame_proses.setVisible(false);
//        jInternalFrame_proses.setTitle("Konfirmasi Pegawai untuk " + actioon);
//    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jInternalFrame_proses = new javax.swing.JInternalFrame();
        jPanel5 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        button_lanjut = new javax.swing.JButton();
        button_proses_pensiun = new javax.swing.JButton();
        nip = new javax.swing.JLabel();
        nama1 = new javax.swing.JLabel();
        keluar_button = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        time = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        nip_br = new javax.swing.JLabel();
        cari_txt = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        combo_jenis = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        P_Nama_Label = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        P_Kode_Label = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        exit_button = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jInternalFrame_proses.setToolTipText("");

        jPanel5.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel4.setText("Nama :");
        jPanel5.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(162, 11, 52, -1));

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel5.setText("NIP    : ");
        jPanel5.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(162, 39, -1, -1));

        button_lanjut.setText("YA");
        button_lanjut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_lanjutActionPerformed(evt);
            }
        });
        jPanel5.add(button_lanjut, new org.netbeans.lib.awtextra.AbsoluteConstraints(53, 153, 98, -1));

        button_proses_pensiun.setText("Proses pensiun");
        button_proses_pensiun.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button_proses_pensiunActionPerformed(evt);
            }
        });
        jPanel5.add(button_proses_pensiun, new org.netbeans.lib.awtextra.AbsoluteConstraints(401, 153, -1, -1));

        nip.setFont(new java.awt.Font("Tahoma", 1, 14));
        jPanel5.add(nip, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 39, 294, 17));

        nama1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jPanel5.add(nama1, new org.netbeans.lib.awtextra.AbsoluteConstraints(220, 11, 294, 17));

        keluar_button.setText("TIDAK");
        keluar_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keluar_buttonActionPerformed(evt);
            }
        });
        jPanel5.add(keluar_button, new org.netbeans.lib.awtextra.AbsoluteConstraints(169, 153, 104, -1));

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel10.setText("Apakah anda yakin untuk melanjutkan?");
        jPanel5.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(53, 118, 461, -1));

        jLabel11.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PIcture/bg1.png"))); // NOI18N
        jPanel5.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 650, 230));

        javax.swing.GroupLayout jInternalFrame_prosesLayout = new javax.swing.GroupLayout(jInternalFrame_proses.getContentPane());
        jInternalFrame_proses.getContentPane().setLayout(jInternalFrame_prosesLayout);
        jInternalFrame_prosesLayout.setHorizontalGroup(
            jInternalFrame_prosesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, 654, Short.MAX_VALUE)
        );
        jInternalFrame_prosesLayout.setVerticalGroup(
            jInternalFrame_prosesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, 231, Short.MAX_VALUE)
        );

        getContentPane().add(jInternalFrame_proses, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 210, 670, 260));

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PIcture/header2.jpg"))); // NOI18N
        jLabel1.setText("jLabel1");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 871, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)
        );

        jPanel1.add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, -1, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 870, 180));

        jPanel3.setBackground(new java.awt.Color(255, 255, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        time.setFont(new java.awt.Font("Tahoma", 1, 14));
        time.setText("time");
        jPanel3.add(time, new org.netbeans.lib.awtextra.AbsoluteConstraints(730, 10, -1, -1));

        jLabel2.setFont(new java.awt.Font("Arial Black", 0, 12));
        jLabel2.setText("Klik salah satu Pegawai untuk proses lain");
        jPanel3.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 260, -1, -1));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "NIP lama", "NIP baru", "Nama lengkap"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jPanel3.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 280, 830, 70));

        jPanel4.setBackground(new java.awt.Color(153, 102, 0));
        jPanel4.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel4.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        nip_br.setFont(new java.awt.Font("Tahoma", 1, 14));
        nip_br.setText("Cari berdasarkan");
        jPanel4.add(nip_br, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));

        cari_txt.setFont(new java.awt.Font("Tahoma", 0, 14));
        cari_txt.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cari_txtKeyReleased(evt);
            }
        });
        jPanel4.add(cari_txt, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 40, 310, -1));

        jButton1.setText("Cari");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel4.add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 90, -1, -1));

        combo_jenis.setFont(new java.awt.Font("Tahoma", 1, 14));
        combo_jenis.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "==pilih==", "NIP Lama", "NIP Baru", "Nama" }));
        combo_jenis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                combo_jenisActionPerformed(evt);
            }
        });
        jPanel4.add(combo_jenis, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 110, -1));

        jLabel9.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PIcture/wall1.jpg"))); // NOI18N
        jPanel4.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, 520, 110));

        jPanel3.add(jPanel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 110, 540, 130));

        jLabel3.setFont(new java.awt.Font("Arial Black", 0, 18));
        jLabel3.setText("Pencarian Pegawai");
        jPanel3.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(330, 30, -1, -1));

        P_Nama_Label.setText("Nama");
        jPanel3.add(P_Nama_Label, new org.netbeans.lib.awtextra.AbsoluteConstraints(160, 10, 120, -1));

        jLabel8.setText("-");
        jPanel3.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 10, -1, -1));

        P_Kode_Label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        P_Kode_Label.setText("Kode");
        jPanel3.add(P_Kode_Label, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 10, 10, -1));

        jLabel6.setText("Selamat Datang,");
        jPanel3.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        exit_button.setFont(new java.awt.Font("Tahoma", 1, 14));
        exit_button.setText("Keluar");
        exit_button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exit_buttonActionPerformed(evt);
            }
        });
        jPanel3.add(exit_button, new org.netbeans.lib.awtextra.AbsoluteConstraints(730, 40, -1, -1));

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/PIcture/wall1.jpg"))); // NOI18N
        jPanel3.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 860, 350));

        getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 180, 870, 360));

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        try {
            String pilihan = (String) combo_jenis.getSelectedItem();
            if (pilihan.matches("==pilih==")) {
                JOptionPane.showMessageDialog(rootPane, "pilih salah satu jenis pencarian", "alert", JOptionPane.WARNING_MESSAGE);
            } else if (pilihan.matches("NIP Lama")) {
                List<PNS> search = ControlData.getKoneksi().SearchPNS_NIPLama(cari_txt.getText());
                if (cari_txt.getText().equalsIgnoreCase("")) {
                    inputKosong();
                } else if (cari_txt.getDocument().getLength() < 9) {
                    JOptionPane.showMessageDialog(rootPane, "NIP Pegawai tidak Lengkap", "perhatian", JOptionPane.WARNING_MESSAGE);
                } else if (cari_txt.getDocument().getLength() > 9) {
                    JOptionPane.showMessageDialog(rootPane, "NIP Pegawai tidak Lengkap", "perhatian", JOptionPane.WARNING_MESSAGE);
                } else if (search.isEmpty()) {
                    JOptionPane.showMessageDialog(rootPane, "data Pegawai tidak ditemukan");
                } else {
                    CariPegawaiTableModel dataPensiun = new CariPegawaiTableModel(search);
                    jTable1.setModel(dataPensiun);
                    jTable1.setVisible(true);
                }
            } else if (pilihan.matches("NIP Baru")) {
                List<PNS> search = ControlData.getKoneksi().SearchPNS_NIPBaru(cari_txt.getText());
                if (cari_txt.getText().equalsIgnoreCase("")) {
                    inputKosong();
                } else if (cari_txt.getDocument().getLength() < 18) {
                    JOptionPane.showMessageDialog(rootPane, "NIP Pegawai tidak Lengkap", "perhatian", JOptionPane.WARNING_MESSAGE);
                } else if (search.isEmpty()) {
                    JOptionPane.showMessageDialog(rootPane, "data Pegawai tidak ditemukan");
                } else {
                    CariPegawaiTableModel dataPensiun = new CariPegawaiTableModel(search);
                    jTable1.setModel(dataPensiun);
                    jTable1.setVisible(true);
                }
            } else {
//                List<PNS> search = ControlData.getKoneksi().SearchPNS_Nama(cari_txt.getText());
//                if (cari_txt.getText().equalsIgnoreCase("")) {
//                    inputKosong();
//                } else if (search.isEmpty()) {
//                    JOptionPane.showMessageDialog(rootPane, "data Pegawai tidak ditemukan");
//                } else {
//                    CariPegawaiTableModel KTM = new CariPegawaiTableModel(search);
//                    jTable1.setModel(KTM);
//                }
                tampilTable(cari_txt.getText());
            }

        } catch (Exception e) {
        }

        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void combo_jenisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_combo_jenisActionPerformed

        String pilihan = (String) combo_jenis.getSelectedItem();
        if (pilihan.matches("==pilih==")) {
            cari_txt.setText("");
            cari_txt.setVisible(false);
            JOptionPane.showMessageDialog(rootPane, "pilih salah satu jenis pencarian", "alert", JOptionPane.WARNING_MESSAGE);
        } else if (pilihan.matches("NIP Lama")) {
            cari_txt.setText("");
            cari_txt.setVisible(true);
            cari_txt.requestFocus();
        } else if (pilihan.matches("NIP Baru")) {
            cari_txt.setText("");
            cari_txt.setVisible(true);
            cari_txt.requestFocus();
        } else {
            cari_txt.setText("");
            cari_txt.setVisible(true);
            cari_txt.requestFocus();
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_combo_jenisActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked

        int row = jTable1.getSelectedRow();
        String nipPeg = jTable1.getValueAt(row, 1).toString();
        String nama = jTable1.getValueAt(row, 2).toString();
        nama1.setText(nama);
        nip.setText(nipPeg);
        jInternalFrame_proses.setVisible(true);

        // TODO add your handling code here:
    }//GEN-LAST:event_jTable1MouseClicked

    private void keluar_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keluar_buttonActionPerformed
        jInternalFrame_proses.setVisible(false);
        cari_txt.setText("");
        combo_jenis.setSelectedItem("==pilih==");
        jTable1.setVisible(false);
        // TODO add your handling code here:
    }//GEN-LAST:event_keluar_buttonActionPerformed

    private void button_proses_pensiunActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_proses_pensiunActionPerformed
        ProsesPensiun pro = new ProsesPensiun();
        pro.isiNIP(nip.getText());
       // pro.isiNIP(nip.getText());
        pro.dataOperator(P_Nama_Label.getText(), P_Kode_Label.getText());
        this.dispose();
        pro.setVisible(true);
        // TODO add your handling code here:
    }//GEN-LAST:event_button_proses_pensiunActionPerformed

    private void exit_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exit_buttonActionPerformed
        //       System.out.println(P_Nama_Label.getText().toUpperCase());
        int status = JOptionPane.showConfirmDialog(rootPane, "Apakah yakin keluar", "Konfirmasi keluar",
                JOptionPane.OK_CANCEL_OPTION);
        if (status == 0) {
            this.dispose();
            FrameOperator FO = new FrameOperator();
            FO.dataOperator(P_Nama_Label.getText(), P_Kode_Label.getText());
            FO.setVisible(true);

        }
        // TODO add your handling code here:
}//GEN-LAST:event_exit_buttonActionPerformed

    private void cari_txtKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cari_txtKeyReleased
//        String pilihan = (String) combo_jenis.getSelectedItem();
//        if (pilihan.matches("NIP Lama")) {
//            inputAngka(cari_txt.getText());
//            cari_txt.setBackground(Color.white);
//        } else if (pilihan.matches("NIP Baru")) {
//            inputAngka(cari_txt.getText());
//            cari_txt.setBackground(Color.white);
//        } else {
//            cari_txt.setBackground(Color.white);
//        }
        // TODO add your handling code here:
    }//GEN-LAST:event_cari_txtKeyReleased

    private void button_lanjutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button_lanjutActionPerformed
        try {
            EditPegawai edit = new EditPegawai(nip.getText());
            edit.dataOperatorEditPegawai(P_Nama_Label.getText(), P_Kode_Label.getText());
            edit.setVisible(true);
            this.dispose();
        } catch (Exception ex) {
            Logger.getLogger(PencarianPegawai.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_button_lanjutActionPerformed
    public void dataOperator(String nama, String id) {
        P_Nama_Label.setText(nama);
        P_Kode_Label.setText(id);
    }

    private void inputAngka(String angka) {
        if (!angka.matches("[0-9]*")) {
            cari_txt.setBackground(Color.red);
            JOptionPane.showMessageDialog(rootPane, "masukan angka", "peringatan", JOptionPane.WARNING_MESSAGE);

        }
    }

    private void tampilTable(String id) {
        try {
            List<PNS> lkat = ControlData.getKoneksi().SearchAllPNS(id);
            CariPegawaiTableModel KTM = new CariPegawaiTableModel(lkat);
            jTable1.setModel(KTM);
        } catch (Exception ex) {
            Logger.getLogger(PencarianPegawai.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new PencarianPegawai().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel P_Kode_Label;
    private javax.swing.JLabel P_Nama_Label;
    private javax.swing.JButton button_lanjut;
    private javax.swing.JButton button_proses_pensiun;
    private javax.swing.JTextField cari_txt;
    private javax.swing.JComboBox combo_jenis;
    private javax.swing.JButton exit_button;
    private javax.swing.JButton jButton1;
    private javax.swing.JInternalFrame jInternalFrame_proses;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton keluar_button;
    private javax.swing.JLabel nama1;
    private javax.swing.JLabel nip;
    private javax.swing.JLabel nip_br;
    private javax.swing.JLabel time;
    // End of variables declaration//GEN-END:variables

    private void inputKosong() {
        JOptionPane.showMessageDialog(rootPane, "Inputan kosong", "", JOptionPane.WARNING_MESSAGE);
        cari_txt.setBackground(Color.red);
        cari_txt.requestFocus();
    }
}
